package com.geo.geotool.service;

import com.geo.geotool.global.Constants;
import org.geotools.data.DataUtilities;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.*;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.renderer.lite.StreamingRenderer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Font;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

public class MapExporter {
    public static void export(List<SimpleFeatureCollection> featureCollections,List<Style> styles, String title
                              ,String secTitle,double x1, double x2, double y1 ,double y2, int imageWidth, int imageHeight, String imgPath) throws IOException {
        try{
            MapContent map = new MapContent();
            addLayer(map,featureCollections.get(0),styles.get(0));


            ReferencedEnvelope mapArea = new ReferencedEnvelope(x1, x2, y1, y2, featureCollections.get(0).getSchema().getCoordinateReferenceSystem());
            // 初始化渲染器
            StreamingRenderer sr = new StreamingRenderer();
            sr.setMapContent(map);
            // 初始化输出图像
            BufferedImage bi = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.getGraphics();
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            Rectangle rect = new Rectangle(0, 0, imageWidth, imageHeight);
            // 绘制地图
            sr.paint((Graphics2D) g, rect, mapArea);

            // 创建标题
            Font titleFont = new Font("SansSerif", Font.BOLD, 24);
            Rectangle2D titleBounds = titleFont.getStringBounds(title, ((Graphics2D) g).getFontRenderContext());
            int titleX = (int) (imageWidth - titleBounds.getWidth()) / 2;
            int titleY = (int) (titleBounds.getHeight()) + 10;
            // 绘制标题
            g.setColor(Color.BLACK);
            g.setFont(titleFont);
            g.drawString(title, titleX, titleY);



            // 创建二级标题
            Font secTitleFont = new Font("SansSerif", Font.BOLD, 16);
            Rectangle2D secTitleBounds = secTitleFont.getStringBounds(secTitle, ((Graphics2D) g).getFontRenderContext());
            int secTitleX = (int) (imageWidth - secTitleBounds.getWidth()) / 2 + 100;
            int secTitleY = (int) (secTitleBounds.getHeight()) + 60;
            // 绘制标题
            g.setColor(Color.BLACK);
            g.setFont(secTitleFont);
            g.drawString(secTitle, secTitleX, secTitleY);


            // 加入图例
            BufferedImage mapImage = ImageIO.read(new File("D:\\test\\image\\legend\\legend.png"));
            int width = mapImage.getWidth();
            int height = mapImage.getHeight();
            // 设置Y的值 - 300  为了让图例显示在地图下方东沙群岛之上
            g.drawImage(mapImage,imageWidth - width , imageHeight - height - 300, null);

            //将BufferedImage变量写入文件中。
            ImageIO.write(bi,"png",new File(imgPath));



        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    private static void addLayer(MapContent map,SimpleFeatureCollection featureCollection,Style style){
        try{
            Layer layer = new FeatureLayer(featureCollection, style);
            System.out.println(layer.getFeatureSource().getFeatures().size());
            map.addLayer(layer);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
